package com.example.xunua_zhinengjiaotong;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_zhinengjiaotong.utils.AmbientBean;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.internal.Util;

public class ambientActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img_back;
    private RelativeLayout ambient_rl;
    private LinearLayout linelayout;
    private TextView tv_pm25,tv_co2,tv_light,tv_humidity,tv_temperature,tv_lightDownUp;
    private ImageView img_pm25,img_co2,img_light,img_humidity,img_temperature,img_set;
    private HttpUtil util=new HttpUtil();
    private MyApp myApp;
    private AmbientBean ambientBean;
    private EditText et_down,et_up;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    tv_co2.setText(ambientBean.getCo2());
                    tv_pm25.setText(ambientBean.getPm25());
                    tv_light.setText(ambientBean.getLightIntensity());
                    tv_humidity.setText(ambientBean.getHumidity());
                    tv_temperature.setText(ambientBean.getTemperature());
                    break;
                case 1:
                    tv_co2.setText(msg.obj.toString());
                    break;
                case 2:
                    tv_pm25.setText(msg.obj.toString());
                    break;
                case 3:
                    tv_light.setText(msg.obj.toString());
                    break;
                case 4:
                    tv_humidity.setText(msg.obj.toString());
                    break;
                case 5:
                    tv_temperature.setText(msg.obj.toString());
                    break;
                case 6:
                    tv_lightDownUp.setText(msg.obj.toString());
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ambient);
        initView();
        getData();
        getDownUp();
    }

    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getAllSense = util.connectServer("{}", myApp, "GetAllSense");
                    String pm25=getAllSense.getString("pm2.5");
                    String co2 = getAllSense.getString("co2");
                    String lightIntensity = getAllSense.getString("LightIntensity");
                    String humidity = getAllSense.getString("humidity");
                    String temperature = getAllSense.getString("temperature");
                    ambientBean=new AmbientBean(pm25,co2,lightIntensity,humidity,temperature);
                    handler.sendEmptyMessage(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private void getDownUp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getLightSenseValve = util.connectServer("{}", myApp, "GetLightSenseValve");
                    String down = getLightSenseValve.getString("Down");
                    String up = getLightSenseValve.getString("Up");
                    Message message=new Message();
                    message.obj=down+"-"+up;
                    message.what=6;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    private void setDownUp(final String Down, final String Up){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    util.connectServer("{\"Down\":\""+Down+"\",\"Up\":\""+Up+"\"}",myApp,"SetLightSenseValve");
                    getDownUp();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        myApp= (MyApp) getApplication();
        img_back=findViewById(R.id.ambient_img_back);
        img_back.setOnClickListener(this);
        ambient_rl=findViewById(R.id.ambient_rl);
        ambient_rl.setOnClickListener(this);
        linelayout=findViewById(R.id.ambient_linelayout);
        tv_pm25=findViewById(R.id.ambient_tv_pm25);
        tv_co2=findViewById(R.id.ambient_tv_co2);
        tv_light=findViewById(R.id.ambient_tv_light);
        tv_humidity=findViewById(R.id.ambient_tv_humidity);
        tv_temperature=findViewById(R.id.ambient_tv_temperature);
        img_co2=findViewById(R.id.ambient_img_co2);
        img_pm25=findViewById(R.id.ambient_img_pm25);
        img_light=findViewById(R.id.ambient_img_light);
        img_humidity=findViewById(R.id.ambient_img_humidity);
        img_temperature=findViewById(R.id.ambient_img_temperature);
        img_co2.setOnClickListener(this);
        img_pm25.setOnClickListener(this);
        img_light.setOnClickListener(this);
        img_humidity.setOnClickListener(this);
        img_temperature.setOnClickListener(this);
        tv_lightDownUp=findViewById(R.id.ambient_tv_lightDownUp);
        et_down=findViewById(R.id.ambient_et_SetLightDown);
        et_up=findViewById(R.id.ambient_et_SetLightUp);
        img_set=findViewById(R.id.ambient_img_setLight);
        img_set.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ambient_img_back:
                finish();
                break;
            case R.id.ambient_rl:
                if (linelayout.getVisibility() == View.VISIBLE) {
                    linelayout.setVisibility(View.GONE);
                }else {
                    linelayout.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.ambient_img_co2:
                getOneData("co2",1);
                break;
            case R.id.ambient_img_pm25:
                getOneData("pm2.5",2);
                break;
            case R.id.ambient_img_light:
                getOneData("LightIntensity",3);
                break;
            case R.id.ambient_img_humidity:
                getOneData("humidity",4);
                break;
            case R.id.ambient_img_temperature:
                getOneData("temperature",5);
                break;
            case R.id.ambient_img_setLight:
                String down = et_down.getText().toString().trim();
                String up = et_up.getText().toString().trim();
                setDownUp(down,up);
                break;
        }
    }

    private void getOneData(final String s, final int i){
        //根据i的值来更新对应的组件 如 i=1代表的是更新co2的值
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getSenseByName = util.connectServer("{\"SenseName\":\"" + s + "\"}", myApp, "GetSenseByName");
                    String OneData = getSenseByName.getString(s);
                    Message message=new Message();
                    message.what=i;
                    message.obj=OneData;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    }
}
